#!/usr/bin/env python3
#coding=utf-8

import os
from distutils.core import setup
from distutils.command.build_scripts import build_scripts

from lonote.version import version

def collect_data_files():
    data_files = [
        ('share/applications', ['xdg/lonote.desktop']),
        ('share/pixmaps', ['icons/lonote.png'])
        ]

    # i18n
    for dir, dirs, files in os.walk('locale'):
        target = os.path.join('share', dir)
        if files:
            files = [os.path.join(dir, f) for f in files]
            data_files.append((target, files))

    # data
    for root_dir in ['example', 'icons', 'default', 'javascript']:
        for dir, dirs, files in os.walk(root_dir):
            target = os.path.join('share', 'lonote', dir)
            if files:
                files = [os.path.join(dir, f) for f in files]
                data_files.append((target, files))

    # docs
    for dir, dirs, files in os.walk('doc'):
        target = os.path.join('share', 'doc', 'lonote', dir)
        if files and dir != 'doc/Win':
            files = [os.path.join(dir, f) for f in files]
            data_files.append((target, files))

    # man
    for dir, dirs, files in os.walk('man'):
        target = os.path.join('share', dir)
        if files:
            files = [os.path.join(dir, f) for f in files]
            data_files.append((target, files))

    return data_files


class lonote_build_scripts(build_scripts):
	'''在 POSIX 中，lonote.pyw 重命名至 lonote'''

	def run(self):
		build_scripts.run(self)
		if os.name == 'posix' and not self.dry_run:
                    file = os.path.join(self.build_dir, 'lonote.pyw')
                    print('renaming %s to %s' % (file, file[:-4]))
                    os.rename(file, file[:-4]) # len('.pyw') == 4


setup(name='lonote',
      version=version,
      description='LoNote is a Note-Taking software which using Python3 and PyQt4',
      author='Larina',
      author_email='larina.wf@gmail.com',
      url='http://code.google.com/p/lonote',
      download_url='http://code.google.com/p/lonote/downloads/list',
      classifiers=[
        'Environment :: X11 Applications :: Qt',
        'Intended Audience :: End Users/Desktop',
        'License :: OSI Approved :: GNU General Public License (GPL)',
        'Programming Language :: Python :: 3',
        ],
      scripts=['lonote.pyw'],
      packages=['lonote', 'lonote.google_dmp'],
      data_files = collect_data_files(),
      cmdclass = {
		'build_scripts': lonote_build_scripts,
	},
     )
